package com.example.ymm.modules1.controller.sys;


import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.example.ymm.model.CommonResult;
import com.example.ymm.model.TableList;
import com.example.ymm.model.YmPageDto;
import com.example.ymm.modules1.controller.BaseController;
import com.example.ymm.modules1.entity.hl.ex.*;
import com.example.ymm.modules1.query.hl.ex.*;
import com.example.ymm.modules1.service.dto.heng.ex.*;
import com.example.ymm.modules1.service.dto.socket.MySocketInfo;
import com.example.ymm.modules1.service.hl.ex.*;
import com.example.ymm.modules1.socket.UserExWebSocket;
import com.example.ymm.schedu.ExTaskScheduler;
import com.example.ymm.util.UtilValidate;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import javax.validation.Valid;
import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

@RestController
@RequestMapping("/hl/ex/manage")
@Api(tags = "后台 试卷管理")
@Slf4j
public class ExManageController  extends BaseController {

    @Autowired
    ExPaperService exPaperService;
    @Autowired
    ExCategoryService exCategoryService;
    @Autowired
    ExQuestionService exQuestionService;

    @Autowired
    ExQuestionOptionService exQuestionOptionService;

    @Autowired
    ExTeamService exTeamService;

    @Autowired
    ExTeamUserService exTeamUserService;

    @Autowired
    ExPublishPaperService exPublishPaperService;
    @Autowired
    ExTaskScheduler exTaskScheduler;

    @Autowired
    ExUserAnserService exUserAnserService;
    @Autowired
    private ExUserPaperService exUserPaperService;

    @Autowired
    ExTeamRuleService exTeamRuleService;

    @Autowired
    ExAnserLogService exAnserLogService;
    @Autowired
    private ExUserGroupService exUserGroupService;
    @Autowired
    private ExPublishGroupService exPublishGroupService;

    @ApiOperation(value = "试卷列表")
    @RequestMapping(value = "/paperList", method = RequestMethod.POST)
    public CommonResult paperList(@Valid ExPaperQueryCriteria criteria, YmPageDto ymPage) {
        try{
            TableList<ExPaper> list = exPaperService.list(criteria, ymPage);
            return CommonResult.success(list,"获取成功");
        }catch (Exception e){
            log.error(e.getMessage(),e);
            return CommonResult.error(e.getMessage());
        }
    }

    @ApiOperation(value = "试卷下拉列表")
    @RequestMapping(value = "/paperSelectList", method = RequestMethod.POST)
    public CommonResult paperSelectList(@Valid ExPaperQueryCriteria criteria) {
        try{
            criteria.setStatus(0);
            List<ExPaper> list = exPaperService.queryAll(criteria);
            return CommonResult.success(list,"获取成功");
        }catch (Exception e){
            log.error(e.getMessage(),e);
            return CommonResult.error(e.getMessage());
        }
    }

    @ApiOperation(value = "团队下拉列表")
    @RequestMapping(value = "/teamSelectList", method = RequestMethod.POST)
    public CommonResult teamSelectList(@Valid ExTeamQueryCriteria criteria) {
        try{
            List<ExTeam> list = exTeamService.queryAll(criteria);
            return CommonResult.success(list,"获取成功");
        }catch (Exception e){
            log.error(e.getMessage(),e);
            return CommonResult.error(e.getMessage());
        }
    }

    @ApiOperation(value = "试卷列表")
    @PostMapping("/paperSave")
    public CommonResult paperSave(@RequestBody ExPaper paper) {
        try{
            if(UtilValidate.isEmpty(paper.getName())){
                return CommonResult.error("请输入完整信息!");
            }
            exPaperService.paperSave(paper);
            return CommonResult.success();
        }catch (Exception e){
            log.error(e.getMessage(),e);
            return CommonResult.error(e.getMessage());
        }
    }


    @ApiOperation(value = "试卷详情")
    @PostMapping("/paperDetail")
    public CommonResult paperDetail(ExPaper paper) {
        try{
            if(UtilValidate.isEmpty(paper.getPaperId())){
                return CommonResult.error("参数异常!");
            }
            ExPaper paper1 = exPaperService.paperDetail(paper);
            return CommonResult.success(paper1);
        }catch (Exception e){
            log.error(e.getMessage(),e);
            return CommonResult.error(e.getMessage());
        }
    }

    @ApiOperation(value = "试卷启用和禁用")
    @RequestMapping(value = "/paperActive", method = RequestMethod.POST)
    public CommonResult paperActive(@RequestParam String paperId,@RequestParam Integer status) {
        try{
            ExPaper paper=new ExPaper();
            paper.setPaperId(paperId);
            paper.setStatus(status);
            exPaperService.updateById(paper);
            return CommonResult.success();
        }catch (Exception e){
            log.error(e.getMessage(),e);
            return CommonResult.error(e.getMessage());
        }
    }

    @ApiOperation(value = "分类列表")
    @RequestMapping(value = "/categoryList", method = RequestMethod.POST)
    public CommonResult categoryList(@Valid ExCategoryQueryCriteria criteria) {
        try{
            List<ExCategory> list = exCategoryService.getCategoryTreeList(criteria);
            return CommonResult.success(list,"获取成功");
        }catch (Exception e){
            log.error(e.getMessage(),e);
            return CommonResult.error(e.getMessage());
        }
    }

    @ApiOperation(value = "分类保存")
    @RequestMapping(value = "/categorySave", method = RequestMethod.POST)
    public CommonResult categorySave(ExCategory category) {
        try{
            if(UtilValidate.isEmpty(category.getCategoryName())){
                return CommonResult.error("请输入完整信息!");
            }
            if(UtilValidate.isEmpty(category.getPid())){
                category.setPid("0");
            }
            if(UtilValidate.isNotEmpty(category.getTeamId())){
                ExTeam exTeam = exTeamService.getById(category.getTeamId());
                if(UtilValidate.isNotEmpty(exTeam)){
                    category.setTeamName(exTeam.getDeptName());
                }
            }
            exCategoryService.saveOrUpdate(category);
            return CommonResult.success();
        }catch (Exception e){
            log.error(e.getMessage(),e);
            return CommonResult.error(e.getMessage());
        }
    }

    @ApiOperation(value = "分类删除")
    @RequestMapping(value = "/categoryRemove", method = RequestMethod.POST)
    public CommonResult categoryRemove(ExCategory category) {
        try{
            exCategoryService.categoryRemove(category);
            return CommonResult.success();
        }catch (Exception e){
            log.error(e.getMessage(),e);
            return CommonResult.error(e.getMessage());
        }
    }


    @ApiOperation(value = "题目列表")
    @RequestMapping(value = "/questionList", method = RequestMethod.POST)
    public CommonResult questionList(@Valid ExQuestionQueryCriteria criteria, YmPageDto ymPage) {
        try{
            TableList<ExQuestion> list = exQuestionService.list(criteria, ymPage);
            return CommonResult.success(list,"获取成功");
        }catch (Exception e){
            log.error(e.getMessage(),e);
            return CommonResult.error(e.getMessage());
        }
    }

    @ApiOperation(value = "题目保存")
    @PostMapping("/questionSave")
    public CommonResult questionSave(@RequestBody ExQuestion question) {
        try{
            if(UtilValidate.isEmpty(question.getTitle())){
                return CommonResult.error("请输入完整信息!");
            }

            exQuestionService.questionSave(question);
            return CommonResult.success();
        }catch (Exception e){
            log.error(e.getMessage(),e);
            return CommonResult.error(e.getMessage());
        }
    }

    @ApiOperation(value = "该题已有人员在以下发布试卷中进行了答题")
    @PostMapping("/getAnsersPublishPaper")
    public CommonResult getAnsersPublishPaper(ExQuestionQueryCriteria criteria) {
        try{
            if(UtilValidate.isEmpty(criteria.getQuestionId())){
                return CommonResult.error("非法参数!");
            }

            List<ExPublishPaper> ansers = exUserAnserService.getAnsersPublishPaper(criteria);
            return CommonResult.success(ansers);
        }catch (Exception e){
            log.error(e.getMessage(),e);
            return CommonResult.error(e.getMessage());
        }
    }


    @ApiOperation(value = "题目修改选项后重新计分")
    @PostMapping("/againCountQuestionScoreFn")
    public CommonResult againCountQuestionScoreFn(ExQuestionQueryCriteria criteria) {
        try{
            if(UtilValidate.isEmpty(criteria.getQuestionId())){
                return CommonResult.error("非法参数!");
            }
            if(UtilValidate.isEmpty(criteria.getPublishPaperId())){
                return CommonResult.error("请选择发布试卷!");
            }
            List<ExUserPaper> exUserPapers = exUserAnserService.againCountQuestionScoreFn(criteria);
            return CommonResult.success(exUserPapers);
        }catch (Exception e){
            log.error(e.getMessage(),e);
            return CommonResult.error(e.getMessage());
        }
    }




    @ApiOperation(value = "题目删除")
    @RequestMapping(value = "/questionRemove", method = RequestMethod.POST)
    public CommonResult questionRemove(ExQuestion question) {
        try{
            exQuestionService.removeById(question);
            return CommonResult.success();
        }catch (Exception e){
            log.error(e.getMessage(),e);
            return CommonResult.error(e.getMessage());
        }
    }

    @ApiOperation(value = "题目批量删除")
    @RequestMapping(value = "/questionBatchRemove", method = RequestMethod.POST)
    public CommonResult questionRemove(@RequestParam String questionIds) {
        try{
            String[] arr = questionIds.split(",");
            for(String id:arr){
                exQuestionService.removeById(id);
                ExQuestionOptionQueryCriteria criteria=new ExQuestionOptionQueryCriteria();
                criteria.setQuestionId(id);
                List<ExQuestionOption> exQuestionOptions = exQuestionOptionService.queryAll(criteria);
                for(ExQuestionOption exQuestionOption:exQuestionOptions){
                    exQuestionOptionService.removeById(exQuestionOption);
                }
            }

            return CommonResult.success();
        }catch (Exception e){
            log.error(e.getMessage(),e);
            return CommonResult.error(e.getMessage());
        }
    }

    //ImportQuestionDataDto dto,
    @ApiOperation(value = "题目导入")
    @PostMapping("/importQuestion")
    public CommonResult importQuestion(@RequestBody ImportQuestionDataDto dto  ) {
        try{
//            System.out.println(dto);
            List<String> msgs = exQuestionService.importQuestionFn(dto);
            return CommonResult.success(msgs);
        }catch (Exception e){
            log.error(e.getMessage(),e);
            return CommonResult.error(e.getMessage());
        }
    }

    @ApiOperation(value = "题目重新排序")
    @PostMapping("/importQuestionSort")
    public CommonResult importQuestionSort(@RequestBody ImportQuestionDataDto dto  ) {
        try{
//            System.out.println(dto);
            List<String> msgs = exQuestionService.importQuestionSortFn(dto);
            return CommonResult.success(msgs);
        }catch (Exception e){
            log.error(e.getMessage(),e);
            return CommonResult.error(e.getMessage());
        }
    }

    @ApiOperation(value = "题目导入2")
    @PostMapping("/importQuestion2")
    public CommonResult importQuestion2(@RequestBody ImportQuestionDataDto2 dto  ) {
        try{
//            System.out.println(dto);
            List<String> msgs = exQuestionService.importQuestionFn2(dto);
            return CommonResult.success(msgs);
        }catch (Exception e){
            log.error(e.getMessage(),e);
            return CommonResult.error(e.getMessage());
        }
    }

    @ApiOperation(value = "题目导入-性格测试题")
    @PostMapping("/importXinGeQuestionFn")
    public CommonResult importXinGeQuestionFn(@RequestBody ImportXinGeQuestionDataDto dto  ) {
        try{
//            System.out.println(dto);
            List<String> msgs = exQuestionService.importXinGeQuestionFn(dto);
            return CommonResult.success(msgs);
        }catch (Exception e){
            log.error(e.getMessage(),e);
            return CommonResult.error(e.getMessage());
        }
    }


    @ApiOperation(value = "题目删除")
    @RequestMapping(value = "/questionOptions", method = RequestMethod.POST)
    public CommonResult questionOptions(@RequestParam String questionId) {
        try{
            ExQuestionOptionQueryCriteria criteria=new ExQuestionOptionQueryCriteria();
            criteria.setQuestionId(questionId);
            List<ExQuestionOption> options = exQuestionOptionService.queryAll(criteria);
            return CommonResult.success(options);
        }catch (Exception e){
            log.error(e.getMessage(),e);
            return CommonResult.error(e.getMessage());
        }
    }


    @ApiOperation(value = "统计分类题目数量")
    @PostMapping("/countByCategoryId")
    public CommonResult countByCategoryId(@RequestParam String categoryId) {
        try{

            Long l = exQuestionService.countByCategoryId(categoryId);
            return CommonResult.success(l);
        }catch (Exception e){
            log.error(e.getMessage(),e);
            return CommonResult.error(e.getMessage());
        }
    }



    @ApiOperation(value = "题目批量设置分类")
    @PostMapping("/questionBatchCategoryId")
    public CommonResult questionBatchCategoryId(@RequestParam String categoryId,@RequestParam String questionIds) {
        try{

            exQuestionService.questionBatchCategoryId(categoryId,questionIds);
            return CommonResult.success();
        }catch (Exception e){
            log.error(e.getMessage(),e);
            return CommonResult.error(e.getMessage());
        }
    }




    @ApiOperation(value = "团队列表")
    @RequestMapping(value = "/teamList", method = RequestMethod.POST)
    public CommonResult teamList(@Valid ExTeamQueryCriteria criteria, YmPageDto ymPage) {
        try{
            TableList<ExTeam> list = exTeamService.list(criteria, ymPage);
            return CommonResult.success(list,"获取成功");
        }catch (Exception e){
            log.error(e.getMessage(),e);
            return CommonResult.error(e.getMessage());
        }
    }

    @ApiOperation(value = "团队保存")
    @PostMapping("/teamSave")
    public CommonResult teamSave(@RequestBody ExTeam team) {
        try{
            if(UtilValidate.isEmpty(team.getName())){
                return CommonResult.error("请输入完整信息!");
            }
            exTeamService.teamSave(team);
            return CommonResult.success();
        }catch (Exception e){
            log.error(e.getMessage(),e);
            return CommonResult.error(e.getMessage());
        }
    }

    @ApiOperation(value = "团队删除")
    @PostMapping("/teamRemove")
    public CommonResult teamRemove( ExTeam team) {
        try{
            if(UtilValidate.isEmpty(team.getTeamId())){
                return CommonResult.error("请输入完整信息!");
            }
            exTeamService.removeById(team);
            return CommonResult.success();
        }catch (Exception e){
            log.error(e.getMessage(),e);
            return CommonResult.error(e.getMessage());
        }
    }

    @ApiOperation(value = "团队详情")
    @PostMapping("/teamDetail")
    public CommonResult teamDetail(ExTeam team) {
        try{
            if(UtilValidate.isEmpty(team.getTeamId())){
                return CommonResult.error("参数异常!");
            }
            ExTeam paper1 = exTeamService.teamDetail(team.getTeamId());
            return CommonResult.success(paper1);
        }catch (Exception e){
            log.error(e.getMessage(),e);
            return CommonResult.error(e.getMessage());
        }
    }

    @ApiOperation(value = "团队导入")
    @PostMapping("/importTeam")
    public CommonResult importTeam(@RequestBody ImportTeamDataDto dto  ) {
        try{
//            System.out.println(dto);
            List<String> msgs = exTeamService.importTeam(dto);
            return CommonResult.success(msgs);
        }catch (Exception e){
            log.error(e.getMessage(),e);
            return CommonResult.error(e.getMessage());
        }
    }
    @ApiOperation(value = "团队成员")
    @PostMapping("/teamUserList")
    public CommonResult teamUserList(@Valid ExTeamUserQueryCriteria criteria) {
        try{
            if(UtilValidate.isEmpty(criteria.getTeamId())){
                return CommonResult.error("参数缺失!");
            }
            List<ExTeamUser> list = exTeamUserService.queryAll(criteria);
            return CommonResult.success(list,"获取成功");
        }catch (Exception e){
            log.error(e.getMessage(),e);
            return CommonResult.error(e.getMessage());
        }
    }


    @ApiOperation(value = "已发布试卷列表")
    @PostMapping("/publishPaperList")
    public CommonResult publishPaperList(ExPublishPaperQueryCriteria criteria, YmPageDto ymPage) {
        try{

            TableList<ExPublishPaper> list = exPublishPaperService.list(criteria, ymPage);
            return CommonResult.success(list);
        }catch (Exception e){
            log.error(e.getMessage(),e);
            return CommonResult.error(e.getMessage());
        }
    }

    @ApiOperation(value = "发布试卷")
    @PostMapping("/addPublishPaper")
    public CommonResult addPublishPaper(@Valid ExPublishPaper exPublishPaper) {
        try{
            if(UtilValidate.isEmpty(exPublishPaper.getPaperId())){
                return CommonResult.error("参数缺失!");
            }
             exPublishPaperService.addPublishPaper(exPublishPaper);
            return CommonResult.success();
        }catch (Exception e){
            log.error(e.getMessage(),e);
            return CommonResult.error(e.getMessage());
        }
    }

    @ApiOperation(value = "发布试卷-启用和禁用")
    @PostMapping("/publishPaperActive")
    public CommonResult publishPaperActive(@RequestParam String publishPaperId, @RequestParam Integer active) {
        try{
            ExPublishPaper publishPaper = exPublishPaperService.getById(publishPaperId);
            if(UtilValidate.isEmpty(publishPaper)){
                return CommonResult.error("参数缺失!");
            }
            ExPublishPaper u_p=new ExPublishPaper();
            u_p.setPublishPaperId(publishPaperId);
            u_p.setActive(active);
            exPublishPaperService.updateById(u_p);
            return CommonResult.success(u_p);
        }catch (Exception e){
            log.error(e.getMessage(),e);
            return CommonResult.error(e.getMessage());
        }
    }

    @ApiOperation(value = "删除发布试卷")
    @PostMapping("/removePublishPaper")
    public CommonResult removePublishPaper(@Valid ExPublishPaper exPublishPaper) {
        try{
            if(UtilValidate.isEmpty(exPublishPaper.getPublishPaperId())){
                return CommonResult.error("参数缺失!");
            }
            exPublishPaperService.removePublishPaper(exPublishPaper);
            return CommonResult.success();
        }catch (Exception e){
            log.error(e.getMessage(),e);
            return CommonResult.error(e.getMessage());
        }
    }

    @ApiOperation(value = "试卷信息")
    @PostMapping("/getPublishPaper")
    public CommonResult getPublishPaper(@Valid ExPublishPaper exPublishPaper) {
        try{
            if(UtilValidate.isEmpty(exPublishPaper.getPublishPaperId())){
                return CommonResult.error("参数缺失!");
            }
            ExPublishPaper publishPaper = exPublishPaperService.getPublishPaper(exPublishPaper);
            return CommonResult.success(publishPaper);
        }catch (Exception e){
            log.error(e.getMessage(),e);
            return CommonResult.error(e.getMessage());
        }
    }


    @ApiOperation(value = "开始考试")
    @PostMapping("/startPublishPaper")
    public CommonResult startPublishPaper(@Valid ExPublishPaper exPublishPaper) {
        try{
            if(UtilValidate.isEmpty(exPublishPaper.getPublishPaperId())){
                return CommonResult.error("参数缺失!");
            }
           exPublishPaperService.startPublishPaper(exPublishPaper);
            return CommonResult.success();
        }catch (Exception e){
            log.error(e.getMessage(),e);
            return CommonResult.error(e.getMessage());
        }
    }

    @ApiOperation(value = "重置")
    @PostMapping("/testTaskScheduler")
    public CommonResult testTaskScheduler(String publishPaperId) {
        try{
            // 计划5分钟后关闭
//            LocalDateTime startTime = LocalDateTime.now();
//            LocalDateTime endTime = startTime.plusMinutes(3);
//            System.out.println("---shutdownTime:" + endTime);
//            ExUserAnserQueryCriteria criteria=new ExUserAnserQueryCriteria();
//            criteria.setPublishPaperId("1935951680408612865");
//            criteria.setUserName("刘庆峰");
//            UserAnserDto userScore = exUserAnserService.getUserScore(criteria);
            exPublishPaperService.recoverPublishPaper(publishPaperId);

            exTaskScheduler.stopTimer();
            //"*/10 * * * * ?"
           // exTaskScheduler.startTimerWithShutdownTime("*/1 * * * * ?", startTime,endTime);
            return CommonResult.success();
        }catch (Exception e){
            log.error(e.getMessage(),e);
            return CommonResult.error(e.getMessage());
        }
    }

    @ApiOperation(value = "手动通知交卷")
    @PostMapping("/sysNotifySubmit")
    public CommonResult sysNotifySubmit(String publishPaperId) {
        try{
            // 计划5分钟后关闭
            exPublishPaperService.shoudongTongzhi( publishPaperId);

            /**
             * 广播交卷
             */

//            UserExWebSocket.broadcastSysSubmitPaper(userPaper);
            return CommonResult.success();
        }catch (Exception e){
            log.error(e.getMessage(),e);
            return CommonResult.error(e.getMessage());
        }
    }

    @ApiOperation(value = "手动获取在线人数")
    @PostMapping("/getScoketUsers")
    public CommonResult getScoketUsers(@RequestParam String publishPaperId) {
        try{
            List<String> userNams=new ArrayList<>();
            UserExWebSocket.userNameMap.keySet().forEach(userName -> {
                MySocketInfo mySocketInfo = UserExWebSocket.userNameMap.get(userName);
                if(UtilValidate.isNotEmpty(mySocketInfo)){
                    if(mySocketInfo.getPublishPaperId().equals(publishPaperId)){
                        userNams.add(mySocketInfo.getUserName());
                    }
                }
            });

            return CommonResult.success(userNams);
        }catch (Exception e){
            log.error(e.getMessage(),e);
            return CommonResult.error(e.getMessage());
        }
    }

    @ApiOperation(value = "试卷答题记录")
    @PostMapping("/getUserAnsers")
    public CommonResult getUserAnsers(@RequestParam String publishPaperId,String userName) {
        try{
            ExUserAnserQueryCriteria criteria=new ExUserAnserQueryCriteria();
            criteria.setPublishPaperId(publishPaperId);
            criteria.setUserName(userName);
            List<UserAnserView> userAnsers = exUserAnserService.getUserAnsers(criteria);
            return CommonResult.success(userAnsers);
        }catch (Exception e){
            log.error(e.getMessage(),e);
            return CommonResult.error(e.getMessage());
        }
    }

    @ApiOperation(value = "0分时根据提交记录重新计算")
    @PostMapping("/againCountByAnserLog")
    public CommonResult againCountByAnserLog(@RequestParam String id) {
        try{
            ExAnserLog exAnserLog = exAnserLogService.getById(id);
            if(exAnserLog==null){
                return CommonResult.error("非法参数!");
            }
            String textJson = exAnserLog.getTextJson();
            ExPublishPaper exPublishPaper= JSON.parseObject(textJson, ExPublishPaper.class);
            exUserAnserService.userSubQuestions(exPublishPaper,exAnserLog.getUserName());
            return CommonResult.success();
        }catch (Exception e){
            log.error(e.getMessage(),e);
            return CommonResult.error(e.getMessage());
        }
    }

    @ApiOperation(value = "查询提交记录")
    @PostMapping("/getCountByAnserLog")
    public CommonResult getCountByAnserLog(@RequestParam String publishPaperId,String userName) {
        try{
            ExAnserLogQueryCriteria criteria=new ExAnserLogQueryCriteria();
            criteria.setPublishPaperId(publishPaperId);
            criteria.setUserName(userName);
            List<ExAnserLog> exAnserLogs = exAnserLogService.queryAll(criteria);
            ExAnserLog exAnserLog = null;
            if(UtilValidate.isNotEmpty(exAnserLogs)){
                exAnserLog = exAnserLogs.get(0);
            }
            return CommonResult.success(exAnserLog);
        }catch (Exception e){
            log.error(e.getMessage(),e);
            return CommonResult.error(e.getMessage());
        }
    }


    @ApiOperation(value = "考试记录")
    @PostMapping("/userPaperList")
    public CommonResult userPaperList(ExUserPaperQueryCriteria criteria, YmPageDto ymPage) {
        try{

            TableList<MyPaperLog> list = exUserPaperService.list(criteria, ymPage);
            return CommonResult.success(list);
        }catch (Exception e){
            log.error(e.getMessage(),e);
            return CommonResult.error(e.getMessage());
        }
    }


    @ApiOperation(value = "获取面试卷的考试结果")
    @PostMapping("/manageUserScoreByMainShi")
    public CommonResult manageUserScoreByMainShi(@RequestParam String userPaperId) {
        try{

            List<GroupOneResultDto> groupOneResultDtos = exUserAnserService.manageUserScoreByMainShi(userPaperId);

            return CommonResult.success(groupOneResultDtos);
        }catch (Exception e){
            log.error(e.getMessage(),e);
            return CommonResult.error(e.getMessage());
        }
    }


    @ApiOperation(value = "面试卷 按照模块来进行重考")
    @PostMapping("/userPaperBrakeGroup")
    public CommonResult userPaperBrakeGroup(@RequestParam String userPaperId,@RequestParam String publishGroupId) {
        try{

            exUserPaperService.userPaperBrakeGroup(userPaperId,publishGroupId);
            return CommonResult.success();
        }catch (Exception e){
            log.error(e.getMessage(),e);
            return CommonResult.error(e.getMessage());
        }
    }

    @ApiOperation(value = "面试卷 整张重考")
    @PostMapping("/userPaperBrakeAll")
    public CommonResult userPaperBrakeAll(@RequestParam String userPaperId) {
        try{
            exUserPaperService.userPaperBrakeAll(userPaperId);
            return CommonResult.success();
        }catch (Exception e){
            log.error(e.getMessage(),e);
            return CommonResult.error(e.getMessage());
        }
    }


    @ApiOperation(value = "获取用户面试卷的下拉分组")
    @PostMapping("/getUserGroups")
    public CommonResult getUserGroups(@RequestParam String userPaperId) {
        try{
            ExUserGroupQueryCriteria criteria=new ExUserGroupQueryCriteria();
            criteria.setUserPaperId(userPaperId);
            List<ExUserGroup> exUserGroups = exUserGroupService.queryAll(criteria);
            return CommonResult.success(exUserGroups);
        }catch (Exception e){
            log.error(e.getMessage(),e);
            return CommonResult.error(e.getMessage());
        }
    }

    @ApiOperation(value = "初始化规则")
    @PostMapping("/initRule")
    public CommonResult initRule() {
        try{

            exTeamRuleService.initRule();
            return CommonResult.success();
        }catch (Exception e){
            log.error(e.getMessage(),e);
            return CommonResult.error(e.getMessage());
        }
    }


    @ApiOperation(value = "初始化分类")
    @PostMapping("/initCategory")
    public CommonResult initCategory() {
        try{
            exCategoryService.initCategory();
            return CommonResult.success();
        }catch (Exception e){
            log.error(e.getMessage(),e);
            return CommonResult.error(e.getMessage());
        }
    }

    @ApiOperation(value = "团队导入")
    @PostMapping("/saveTeamRules")
    public CommonResult saveTeamRules(@RequestBody ExTeam team  ) {
        try{
//            System.out.println(dto);
           exTeamRuleService.saveTeamRules(team);
            return CommonResult.success();
        }catch (Exception e){
            log.error(e.getMessage(),e);
            return CommonResult.error(e.getMessage());
        }
    }


    @ApiOperation(value = "用户获取试卷分数")
    @PostMapping("/getUserScore")
    public CommonResult getUserScore(@RequestParam String userPaperId) {
        try{
            ExUserPaper userPaper = exUserPaperService.getById(userPaperId);
            if(UtilValidate.isEmpty(userPaper)){
                return CommonResult.error("数据有误!");
            }
            UserAnserDetailDto dto= exPublishPaperService.getUserScore(userPaper.getPublishPaperId(),userPaper.getUserName() );
            return CommonResult.success(dto);
        }catch (Exception e){
            log.error(e.getMessage(),e);
            return CommonResult.error(e.getMessage());
        }
    }


    @ApiOperation(value = "修改发布模块")
    @PostMapping("/savePublishGroup")
    public CommonResult savePublishGroup(@RequestBody ExPublishGroup exPublishGroup) {
        try{
            if(UtilValidate.isEmpty(exPublishGroup.getPublishGroupId())){
                return CommonResult.error("参数缺失!");
            }
            exPublishGroupService.savePublishGroup(exPublishGroup);
            return CommonResult.success();
        }catch (Exception e){
            log.error(e.getMessage(),e);
            return CommonResult.error(e.getMessage());
        }
    }


    @ApiOperation(value = "重考发布模块")
    @PostMapping("/againPublishGroup")
    public CommonResult againPublishGroup(@RequestBody ExPublishGroup exPublishGroup) {
        try{
            if(UtilValidate.isEmpty(exPublishGroup.getPublishGroupId())){
                return CommonResult.error("参数缺失!");
            }
            exPublishGroupService.againPublishGroup(exPublishGroup);
            return CommonResult.success();
        }catch (Exception e){
            log.error(e.getMessage(),e);
            return CommonResult.error(e.getMessage());
        }
    }


}
